/**
 * 
 */
package cn.erp.app.system.controller;

import java.util.ArrayList;
import java.util.List;

import cn.erp.app.service.UserService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SystemMenu;
import cn.erp.common.model.SystemUser;

import com.jfinal.common.controller.BaseController;
import com.jfinal.common.util.DateTimeFormat;
import com.jfinal.common.util.MD5Utils;
import com.jfinal.ext.ret.ReqResult;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author YuanZhiHua
 *
 */
@ControllerBind(controllerKey = "/system/user", viewPath = "/system/user")
public class UserController extends BaseController {
	public void index() {
		// List<SystemUser> users = SystemUser.dao
		// .find("select *from system_user");
		// setAttr("menus", getSession().getAttribute("menus"));
		render("user_index.html");
	}

	public void saveorupdate() {
		SystemUser systemUser = getModel(SystemUser.class, "");
		// String password2 = getPara("password2");

		try {

			// if (!password2.equals(systemUser.getPassword())) {
			// renderJson(ReqResult.ADD_ERROR.setMsg("两次输入的密码不一致！"));
			// return;
			// }

			if (systemUser.getPassword() != null) {
				systemUser
						.setPassword(MD5Utils.getPwd(systemUser.getPassword()));
			}
			if (systemUser.getId() != null && systemUser.getId() != 0) {
				systemUser.update();
			} else {
				SystemUser user = SystemUser.dao.findUserByLoginName(systemUser
						.getLoginName());
				if (user != null) {
					renderJson(ReqResult.ADD_ERROR.setMsg("用户已经存在！"));
					return;
				}
				systemUser.setCreateDate(DateTimeFormat.getDateTime());
				systemUser.save();
			}
			renderJson(ReqResult.ADD_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.SYSTEM_EXCEPTION);
	}

	public void search() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		SystemUser user = getModel(SystemUser.class, "usr");

		String where = "from system_user where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (user.getLoginName() != null) {
			where += " and loginName = ? ";
			objs.add(user.getLoginName());
		}
		if (user.getFullName() != null) {
			where += " and  fullName = ? ";
			objs.add(user.getFullName());
		}
		System.out.println(objs.toArray());
		Page<SystemUser> sysuser = SystemUser.dao.paginate(pageNumber,
				pageSize, "select * ", where, objs.toArray());
		render(EuiRender.page(sysuser));
	}

	/**
	 * 重置用户密码
	 */
	public void resetpwd() {
		long id = getParaToLong("id");
		try {
			SystemUser user = SystemUser.dao.findById(id);
			if (user != null) {
				user.setPassword(MD5Utils.getPwd("888888"));
				user.update();
				renderJson(ReqResult.RESET_SUCCESS);
			} else {
				renderJson(ReqResult.RESET_FAILED.setMsg("用户不存在！"));
			}

			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.RESET_FAILED);
	}

	public void getdata() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 50);
		SystemUser user = getModel(SystemUser.class, "usr");

		String where = "from system_user where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (user.getLoginName() != null) {
			where += " and loginName = ? ";
			objs.add(user.getLoginName());
		}
		if (user.getFullName() != null) {
			where += " and  fullName = ? ";
			objs.add(user.getFullName());
		}
		System.out.println(objs.toArray());
		Page<SystemUser> sysuser = SystemUser.dao.paginate(pageNumber,
				pageSize, "select * ", where, objs.toArray());
		// renderJson((result));
		render(EuiRender.page(sysuser));
//		return;
		// renderJson(ReqResult.QUERY_DETAIL_FAILED);
	}

	public void delete() {
		String[] ids = getParaValues("ids");
		try {
			if (ids != null && !ids.equals("")) {
				for (String id : ids) {
					SystemUser.dao.deleteById(id);
				}
			}
			renderJson(ReqResult.DEL_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.DEL_ERROR);

	}

	public void edit() {
		long id = getParaToLong("id");
		try {
			SystemUser user = SystemUser.dao.findById(id);
			renderJson(user);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.UPDATE_FAILED);
	}
}
